﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rocket.Windows.Framework;
using Rocket.Bll.Models;
using Rocket.Core.Common;
using Rocket.Core;

namespace MainForm.ChildControls
{
    public partial class ReaderApplicationQuestionData : BaseDataEditor
    {
        public ReaderApplicationQuestionData()
        {
            InitializeComponent();
        }

        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);
            this.acceptedAnswerCodeCombo1.LoadData();
            this.ansValidationCodeCombo1.LoadData();
            this.questionListCombo1.LoadData();
        }

        public override void SetRocketBindingSource(ref BindingSource RocketBindingSource)
        {
            RocketBindingSource = this.questionListBindingSource;
        }

        public override V MapIt<V>(int Id, V bindedView)
        {
            BaseObject appView = bindedView;
            if (Id == 0)
                appView = new ReaderApplicationQuestionView();
            ((ReaderApplicationQuestionView)appView).AcceptedAnswersCodeType = (AcceptedAnswerCodes)acceptedAnswerCodeCombo1.SelectedValue;
            ((ReaderApplicationQuestionView)appView).AnswersValidationCodeType = (AnswerValidationCodes)ansValidationCodeCombo1.SelectedValue;
            ((ReaderApplicationQuestionView)appView).Id = Id;
            ((ReaderApplicationQuestionView)appView).IsLastQuestion = isLastQuestionCheckBox.Checked;
            ((ReaderApplicationQuestionView)appView).IsRepeating = isRepeatingCheckBox.Checked;
            ((ReaderApplicationQuestionView)appView).QuestionId = ((QuestionView)this.questionListCombo1.SelectedValue).Id;
            ((ReaderApplicationQuestionView)appView).QuestionText = ((QuestionView)this.questionListCombo1.SelectedValue).Text;
            ((ReaderApplicationQuestionView)appView).ReaderApplicationId = ((ChildForm<ReaderApplicationView>)this.ParentForm).ParenetObjectView.Id;
            ((ReaderApplicationQuestionView)appView).SendDefaultAnswer = sendDefaultAnswerCheckBox.Checked;
            ((ReaderApplicationQuestionView)appView).Sequence = DataManager.ConvertToInt(sequenceTextBox.Text);
            ((ReaderApplicationQuestionView)appView).WaitingCount = DataManager.ConvertToInt(waitingCountTextBox.Text);
            return (V)appView;
        }

        public override void RocketBindingSourceChanged(object sender, EventArgs e)
        {
            if (sender != null)
            {
                this.ansValidationCodeCombo1.SelectedValue = ((ReaderApplicationQuestionView)sender).AnswerValidationCode;
                this.acceptedAnswerCodeCombo1.SelectedValue = ((ReaderApplicationQuestionView)sender).AcceptedAnswerCode;
                this.questionListCombo1.SelectedIndex = this.questionListCombo1.FindString(((ReaderApplicationQuestionView)sender).QuestionText);
            }
        }

        private void buttonquestionListCombo1_Click(object sender, EventArgs e)
        {
            QuestionDataManager questionManager = new QuestionDataManager();
            questionManager.Dock = DockStyle.Fill;
            Form questionForm = new BaseForm();
            questionForm.Width = this.Parent.Width;
            questionForm.Height = 400;
            questionForm.Controls.Add(questionManager);
            questionForm.StartPosition = FormStartPosition.CenterParent;
            questionForm.Text = "Question List Edit";
            questionForm.ShowDialog(this);
            this.questionListCombo1.LoadData();
        }
    }
}
